package com.mpsc.seata.provite.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class StringUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {
        String json ="{\"taskId\":\"3354240135259269879\",\"exectionId\":null,\"procDefId\":\"bm_prj_wf:50:bc1be6b3-e6dc-11ea-95a3-fa163ed561dd\",\"procInstId\":\"3354240135246686960\",\"nodeId\":\"sid-622F20F4-7D66-4DAA-B998-010C026CAE8F\",\"nodeName\":null,\"status\":\"next\",\"operateType\":\"input\",\"businessKey\":\"1860060002221002\",\"bizTypeCode\":\"101003\",\"jsonObj\":\"{\\\"appCode\\\":\\\"101\\\",\\\"appName\\\":\\\"\\\",\\\"bizTypeCode\\\":\\\"101003\\\",\\\"bizTypeName\\\":\\\"\\\",\\\"taskId\\\":\\\"3354240135259269879\\\",\\\"toUserRoleIds\\\":\\\"3347670269951721960,3347670310592430569\\\",\\\"logicParentNodeTaskId\\\":\\\"\\\",\\\"parentTaskId\\\":null,\\\"menus\\\":\\\"[{\\\\\\\"menuId\\\\\\\":\\\\\\\"3339312221263743476\\\\\\\",\\\\\\\"menuName\\\\\\\":\\\\\\\"101004003 项目申报\\\\\\\"}]\\\",\\\"variables\\\":\\\"{\\\\\\\"organizationId\\\\\\\":\\\\\\\"3333294864504432640\\\\\\\",\\\\\\\"prjTask\\\\\\\":\\\\\\\"67\\\\\\\",\\\\\\\"operateState\\\\\\\":\\\\\\\"next_create\\\\\\\",\\\\\\\"agency_code\\\\\\\":\\\\\\\"186006\\\\\\\",\\\\\\\"menuCode\\\\\\\":\\\\\\\"3339312221263743476\\\\\\\",\\\\\\\"agencybgt_id\\\\\\\":\\\\\\\"3347695531707843388\\\\\\\",\\\\\\\"operateType\\\\\\\":\\\\\\\"input\\\\\\\",\\\\\\\"businessKey\\\\\\\":\\\\\\\"1860060002221002\\\\\\\",\\\\\\\"fiscalYear\\\\\\\":\\\\\\\"2021\\\\\\\",\\\\\\\"userId\\\\\\\":\\\\\\\"319900000186001001\\\\\\\",\\\\\\\"MofDep_id\\\\\\\":\\\\\\\"12\\\\\\\"}\\\",\\\"nodeVariables\\\":{\\\"data_type\\\":\\\"0\\\"}}\"}";
        Map<String,Object> variables = new HashMap<>();
        executionProcEvent(json,variables);

    }

    /**
     * 组装缺少参数
     * @param json 任务Id
     * @param variables 参数
     * */
    public static String executionProcEvent(String json,Map<String,Object> variables) throws JsonProcessingException {
        ProcEventRequestObj procEvent = JSONObject.parseObject(json,ProcEventRequestObj.class);
        Map<String,Object> jsonMap = JSONObject.parseObject(procEvent.getJsonObj(),Map.class);
        String variableMap = (String)jsonMap.get("variables");
        Map<String,Object> variablesMap = JSONObject.parseObject(variableMap,Map.class);
        for(String key : variables.keySet()){
            variablesMap.put(key,variables.get(key));
        }
        String variablesStr = objectMapper.writeValueAsString(variablesMap);
        Map<String,Object> map = new HashMap<>();
        for(String key : jsonMap.keySet()){
            map.put(key,jsonMap.get(key));
        }
        map.put(variableMap,variablesStr);
        procEvent.setJsonObj(JSONObject.toJSONString(map));
        return JSONObject.toJSONString(procEvent);
    }
}
